﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ZLCMS.Web.SystemManage.Test.Sample
{
    public partial class SampleList : System.Web.UI.Page
    {
        protected ZLCMS.Business.DwzFrame.FrameHelper _framePage;

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);
            
            //设置分页大小
            int pageSize = _framePage.IsAjaxPagerPostBack ? _framePage.PageSize : 20;

            //设置页码
            int pageIndex = _framePage.IsAjaxPagerPostBack ? _framePage.PageIndex : 1;

            using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                //判断是否导出EXCEL操作
                if (!string.IsNullOrEmpty(Request["exportexcel"]))
                {
                    ExportExcel();
                }

                //设置记录总数
                _framePage.RecordCount = Convert.ToInt32(session.CreateQuery("select count(*) from --YourObject--").UniqueResult());
                
                //设置数据源
                repeater1.DataSource = session.CreateQuery("from --YourObject--")
                    .SetFirstResult((pageIndex - 1) * pageSize)
                    .SetMaxResults(pageSize)
                    .List<--YourObject-->();
            }

            repeater1.DataBind();
        }


        protected void ExportExcel()
        {
            using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<--YourObject-->();
                SetupCondition(ref criteria);
                criteria.AddOrder(Order.Desc("Id"));

                IList<--YourObject--> list = criteria.List<--YourObject-->();


                StringWriter sw = new StringWriter();
                sw.WriteLine("列1\t列2\t列3\t列4");
                foreach (--YourObject-- item in list)
                {
                    sw.WriteLine("'" + item.CheckCode + "\t" + item.OperatorId + "\t" + item.UseDate + "\t" + ParamKit.GetValue("YesNo_10", item.Status, ""));
                }
                sw.Close();

                OutputKit.OutputExcel(Response, sw);
            }
        }
    }
}